<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:">
      <el-input v-model="userInfo.password" disabled type="password" style="width:300px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hSubmit">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getUserInfoById, saveUserDetailById } from '@/api/user'
export default {
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      }
    }
  },
  created() {
    this.loadUserInfoById()
  },
  methods: {
    // 获取用户的个人信息
    async loadUserInfoById() {
      try {
        const res = await getUserInfoById(this.$route.params.id)
        console.log('getUserInfoById', res)
        this.userInfo = res.data
        // 保存数据
      } catch (err) {
        console.log('getUserInfoById', err)
      }
    },
    // 更新用户名和密码
    async hSubmit() {
      // 兜底检验
      try {
        const res = await saveUserDetailById(this.userInfo)
        console.log('saveUserDetailById', res)
        // 调回员工管理页面
        this.$router.back()
      } catch (err) {
        console.log('', err)
      }
    }
  }

}
</script>
